<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
  <th:block th:include="include :: header('用户修改')"/>
</head>
<body>
<form class="layui-form" action="">
  <div class="mainBox">
    <div class="main-container">
      <div class="main-container">
        <div class="layui-form-item layui-hide">
          <label class="layui-form-label">编号</label>
          <div class="layui-input-block">
            <input type="text" th:value="${sysUser.adminId}" name="adminId" lay-verify="title"
                   autocomplete="off" placeholder="请输入 " class="layui-input">
          </div>
        </div>
        <div class="layui-form-item">
          <label class="layui-form-label">账号</label>
          <div class="layui-input-block">
            <input type="text" th:value="${sysUser.name}" name="name" lay-verify="required"
                   autocomplete="off" placeholder="请输入 " class="layui-input">
          </div>
        </div>
        <div class="layui-form-item">
          <label class="layui-form-label">姓名</label>
          <div class="layui-input-block">
            <input type="text" th:value="${sysUser.realName}" name="realName"
                   autocomplete="off" placeholder="请输入 " class="layui-input">
          </div>
        </div>
        <div class="layui-form-item">
          <label class="layui-form-label">密码</label>
          <div class="layui-input-block">
            <input type="password" name="password"
                   autocomplete="off" placeholder="请输入 " class="layui-input">
          </div>
        </div>
        <div class="layui-form-item">
          <label class="layui-form-label">性别</label>
          <div class="layui-input-block">
            <select name="sex" lay-verify="required">
              <option value=""></option>
              <option value="" th:each="sex : ${dictSex}" th:value="${sex.dataValue}"
                      th:selected="${sysUser.sex==sex.dataValue}" th:text="${sex.dataLabel}"></option>
            </select>
          </div>
        </div>
        <div class="layui-form-item">
          <label class="layui-form-label">角色</label>
          <div class="layui-input-block">
            <input th:each="sysRole:${sysRoles}" th:checked="${sysRole.checked}"
                   th:value="${sysRole.roleId}" th:title="${sysRole.roleName}" type="checkbox"
                   name="roleIds" lay-skin="primary">
          </div>
        </div>
      </div>
    </div>
  </div>
  <div class="bottom">
    <div class="button-container">
      <button type="submit" class="pear-btn pear-btn-primary submit pear-btn-sm" lay-submit=""
              lay-filter="user-update">
        <i class="layui-icon layui-icon-ok"></i>
        提交
      </button>
    </div>
  </div>
</form>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">
  layui.use(['form', 'jquery', 'button', 'popup'], function () {
    let form = layui.form;
    let $ = layui.jquery;
    var button = layui.button;
    var popup = layui.popup;


    form.on('submit(user-update)', function (data) {
      let roleIds = "";
      $('input[type=checkbox]:checked').each(function () {
        roleIds += $(this).val() + ",";
      });
      roleIds = roleIds.substr(0, roleIds.length - 1);
      data.field.roleIds = roleIds;
      let btn = button.load({elem: '.submit'});
      $.ajax({
        url: suffix + '/admin/update',
        data: data.field,
        dataType: 'json',
        type: 'post',
        success: function (result) {
          btn.stop({
            text: "提交",
            elem: '.submit',
            time: 1000,
            done: function () {
              if (result.success) {
                layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                  parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                  parent.layui.table.reload("user-table");
                });
              } else {
                layer.msg(result.msg, {icon: 2, time: 1000});
              }
            }
          })
        }
      })
      return false;
    });
  })
</script>
</body>
</html>
